package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.PayTransSummaryMapper;
import com.net.xpay.core.domain.PayTransSummary;
import com.net.xpay.core.enums.PayTransBatchStatus;
import com.net.xpay.core.manager.PayTransSummaryManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.List;


@Component
@ReadTransactional
public class PayTransSummaryManagerImpl extends AbstractVersionedManager<PayTransSummary> implements PayTransSummaryManager{

    @Autowired
    private PayTransSummaryMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(PayTransSummary obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        //商户的批次号
        obj.setOutBatchNo(obj.getUserId() + "-" + obj.getId());
        return super.save(obj);
    }


    @Override
    protected BasicMapper<Long, PayTransSummary> getMapper() {
        return mapper;
    }

    @Override
    public PayTransSummary findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        PayTransSummary condition = new PayTransSummary();
        condition.setId(id);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<PayTransSummary> listByStatuses(List<PayTransBatchStatus> statuses, LocalDateTime minCreateTime, LocalDateTime maxCreateTime) {
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(statuses));
        return nonEmptyList(mapper.listByStatuses(statuses, minCreateTime, maxCreateTime));
    }
}
